package di

import (
	"gitee.com/ichub/goconfig/common/base/baseiface"
	"gitee.com/ichub/goconfig/common/base/baseutils/fileutils"
	"gitee.com/ichub/goconfig/common/base/baseutils/jsonutils"
	"gitee.com/ichub/goconfig/common/ichublog"
	"gitee.com/ichub/godi/di/didto"
	"gitee.com/ichub/godi/di/difactroy"
	"github.com/sirupsen/logrus"
	"github.com/stretchr/testify/suite"
	"testing"
)

type TestDiFactoySuite struct {
	suite.Suite
	DiFactroy *difactroy.DiFactroy
	rootdir   string
}

func (this *TestDiFactoySuite) SetupTest() {
	logrus.Info("difactry...")
	ichublog.InitLogrus()
	this.rootdir = fileutils.FindRootDir()
	var i baseiface.ISingleton = nil
	logrus.Info(i)
	this.DiFactroy = difactroy.NewDiFactroy()
}

func TestDiFactoySuites(t *testing.T) {
	suite.Run(t, new(TestDiFactoySuite))
}

// 生成所有继承baseentity的结果
func (this *TestDiFactoySuite) Test002_MakeDiAll() {

	//this.DiFactroy.MakeDiAll()
	this.DiFactroy.MakeDiAllForce(false)

}

func (this *TestDiFactoySuite) Test001_MakeDiOne() {
	//	this.DiFactroy.ParseAll()
	var fileinfo = this.DiFactroy.Parse("./difactroy/DiFactroy.go")
	fileinfo.StructInfos[0].ForceBuild = true
	this.DiFactroy.MakeBatch(fileinfo.StructInfos[0])
}

// 生成所有继承baseentity的结果
func (this *TestDiFactoySuite) Test003_MakeDiAllForce() {

	//this.DiFactroy.MakeDiAll()
	this.DiFactroy.MakeDiAllForce(true)

}

func (this *TestDiFactoySuite) Test004_Makes1() {

	var fileinfo = this.DiFactroy.Parse("./difactroy/s1.go")
	this.DiFactroy.MakeBatch(fileinfo.StructInfos[0])
}
func (this *TestDiFactoySuite) Test005_ParseAll() {

	this.DiFactroy.ParseAll()
	var cf, found = this.DiFactroy.FindSome("SingleEntity")
	if found {
		logrus.Error("\r\n", cf.PathFile)
		logrus.Info(cf.ToString())
	} else {
		logrus.Error("\r\n!found ", found)
	}
}

// find is not support multi entity
func (this *TestDiFactoySuite) Test007_FindSingle() {
	//var bean1 = single.FindBeanSingleEntity()
	//
	//bean1.Name = "111"
	//var bean2 = single.FindBeanSingleEntity()
	//var bean3 = single.FindBeanSingleEntity()
	//bean2.Name = "222"
	//logrus.Info(bean1, bean2, bean3)
	//this.Equal(bean1.Name == bean2.Name, true)

}

func (this *TestDiFactoySuite) Test008_MakeDi() {
	var dto = didto.FindBeanDiDto()
	dto.PkgName = "didto"
	dto.StructName = "DiDto"
	this.DiFactroy.MakeDi(dto)

}

func (this *TestDiFactoySuite) Test009_FindFs() {
	this.DiFactroy.FindGoFiles()
	for _, v := range this.DiFactroy.FileInfoDtos {
		this.DiFactroy.Parse(v.PathFile)

	}
}

func (this *TestDiFactoySuite) Test010_MakeDiOne() {

	var fileinfo = this.DiFactroy.Parse(this.rootdir + "/common/ichubcontext/ichub_context.go")
	this.DiFactroy.MakeBatch(fileinfo.StructInfos[0])
}

func (this *TestDiFactoySuite) Test011_FindGofiles() {
	this.DiFactroy.FindGoFiles()

	logrus.Info("basedi=", jsonutils.ToJsonPretty(this.DiFactroy))
}
